package com.bjbn.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bjbn.dto.BmsApiResponse;
import com.bjbn.dto.BmsDeviceGroupDto;
import com.bjbn.dto.BmsDeviceInfoDto;
import com.bjbn.dto.BmsDeviceValueDto;
import com.bjbn.entity.BmsDeviceGroup;
import com.bjbn.entity.BmsDeviceInfo;
import com.bjbn.entity.BmsDeviceValue;
import com.bjbn.entity.BmsDeviceValueThs;
import com.bjbn.mapper.BmsDeviceGroupMapper;
import com.bjbn.mapper.BmsDeviceInfoMapper;
import com.bjbn.mapper.BmsDeviceValueMapper;
import com.bjbn.mapper.BmsDeviceValueThsMapper;
import com.bjbn.service.BmsApiService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * BMS对外查询接口
 */
@RestController
@RequestMapping("/api/bms")
@RequiredArgsConstructor
@Tag(name = "BMS查询接口", description = "BMS系统数据查询相关接口")
public class BmsQueryController {
    private final BmsDeviceGroupMapper deviceGroupMapper;
    private final BmsDeviceInfoMapper deviceInfoMapper;
    private final BmsDeviceValueMapper deviceValueMapper;
    private final BmsDeviceValueThsMapper deviceValueThsMapper;
    private final BmsApiService bmsApiService;

    /**
     * 查询所有设备分组
     */
    @Operation(summary = "查询所有设备分组", description = "获取BMS系统中所有的设备分组信息")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功")
    })
    @GetMapping("/device-groups")
    public List<BmsDeviceGroup> getAllDeviceGroups() {
        return deviceGroupMapper.selectList(null);
    }

    /**
     * 查询指定设备的所有属性
     */
    @Operation(summary = "查询指定设备信息", description = "根据设备编号查询设备的所有属性信息")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "404", description = "未找到指定设备")
    })
    @GetMapping("/device-info/{deviceNum}")
    public List<BmsDeviceInfo> getDeviceInfo(
            @Parameter(description = "设备编号", required = true, example = "DEV001")
            @PathVariable String deviceNum) {
        LambdaQueryWrapper<BmsDeviceInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BmsDeviceInfo::getDeviceNum, deviceNum);
        return deviceInfoMapper.selectList(wrapper);
    }

    /**
     * 查询指定设备的最新采集值
     */
    @Operation(summary = "查询设备最新采集值", description = "根据设备编号查询设备的最新采集值数据")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "404", description = "未找到指定设备")
    })
    @GetMapping("/device-value/{deviceNum}")
    public BmsDeviceValue getDeviceValue(
            @Parameter(description = "设备编号", required = true, example = "DEV001")
            @PathVariable String deviceNum) {
        LambdaQueryWrapper<BmsDeviceValue> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BmsDeviceValue::getDeviceNum, deviceNum)
                .orderByDesc(BmsDeviceValue::getCreateTime)
                .last("limit 1");
        return deviceValueMapper.selectOne(wrapper);
    }

    /**
     * 查询温湿度传感器最新数据
     */
    @Operation(summary = "查询温湿度传感器最新数据", description = "根据设备编号查询温湿度传感器的最新数据")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "404", description = "未找到指定设备")
    })
    @GetMapping("/ths/latest/{deviceNum}")
    public BmsDeviceValueThs getThsLatestValue(
            @Parameter(description = "设备编号", required = true, example = "THS001")
            @PathVariable String deviceNum) {
        LambdaQueryWrapper<BmsDeviceValueThs> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BmsDeviceValueThs::getDeviceNum, deviceNum);
        return deviceValueThsMapper.selectOne(wrapper);
    }

    /**
     * 查询温湿度传感器历史数据 - 已禁用，减轻服务器压力
     */
    /*
    @GetMapping("/ths/history/{deviceNum}")
    public List<BmsDeviceValueThsHis> getThsHistoryValue(@PathVariable String deviceNum) {
        LambdaQueryWrapper<BmsDeviceValueThsHis> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BmsDeviceValueThsHis::getDeviceNum, deviceNum)
                .orderByDesc(BmsDeviceValueThsHis::getCreateTime);
        return deviceValueThsHisMapper.selectList(wrapper);
    }
    */

    /**
     * 查询温湿度传感器历史数据（分页）- 已禁用，减轻服务器压力
     */
    /*
    @GetMapping("/ths/history/{deviceNum}/page")
    public List<BmsDeviceValueThsHis> getThsHistoryValuePage(
            @PathVariable String deviceNum,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "100") Integer size) {
        LambdaQueryWrapper<BmsDeviceValueThsHis> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BmsDeviceValueThsHis::getDeviceNum, deviceNum)
                .orderByDesc(BmsDeviceValueThsHis::getCreateTime)
                .last("limit " + (page - 1) * size + "," + size);
        return deviceValueThsHisMapper.selectList(wrapper);
    }
    */

    /**
     * 实时获取设备属性（直连BMS接口）
     */
    @Operation(summary = "实时获取设备分组", description = "直连BMS接口实时获取设备分组信息")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "500", description = "BMS接口调用失败")
    })
    @GetMapping("/realtime/device-group")
    public List<BmsDeviceGroupDto> getRealtimeDeviceGroup() {
        BmsApiResponse<List<BmsDeviceGroupDto>> apiResponse = bmsApiService.getAllDeviceList();
        return apiResponse.getData();
    }

    /**
     * 实时获取设备属性（直连BMS接口）
     */
    @Operation(summary = "实时获取设备信息", description = "直连BMS接口实时获取指定设备的属性信息")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "404", description = "未找到指定设备"),
            @ApiResponse(responseCode = "500", description = "BMS接口调用失败")
    })
    @GetMapping("/realtime/device-info/{deviceNum}")
    public List<BmsDeviceInfoDto> getRealtimeDeviceInfo(
            @Parameter(description = "设备编号", required = true, example = "DEV001")
            @PathVariable String deviceNum) {
        BmsApiResponse<List<BmsDeviceInfoDto>> apiResponse = bmsApiService.getDeviceTagByDeviceId(deviceNum);
        return apiResponse.getData();
    }

    /**
     * 实时获取设备采集值（直连BMS接口）
     */
    @Operation(summary = "实时获取设备采集值", description = "直连BMS接口实时获取指定设备的采集值数据")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "404", description = "未找到指定设备"),
            @ApiResponse(responseCode = "500", description = "BMS接口调用失败")
    })
    @GetMapping("/realtime/device-value/{deviceNum}")
    public BmsDeviceValueDto getRealtimeDeviceValue(
            @Parameter(description = "设备编号", required = true, example = "DEV001")
            @PathVariable String deviceNum) {
        BmsApiResponse<BmsDeviceValueDto> apiResponse = bmsApiService.getDeviceValueByDeviceId(deviceNum);
        return apiResponse.getData();
    }
} 